Internet Subscriptions#

ict_stats <- read.csv("ict_stats.csv")
str(ict_stats)
'data.frame':	44 obs. of  4 variables:
 $ Year   : int  2011 2011 2011 2011 2012 2012 2012 2012 2013 2013 ...
 $ Quarter: chr  "Q1" "Q2" "Q3" "Q4" ...
 $ ADSL   : int  14082 14419 14474 15707 16298 17204 18166 18838 19388 23224 ...
 $ Mobile : int  189803 200198 224474 238942 263131 294548 509926 769805 958074 1098523 ...
summary(ict_stats)
      Year        Quarter               ADSL            Mobile       
 Min.   :2011   Length:44          Min.   : 14082   Min.   : 189803  
 1st Qu.:2013   Class :character   1st Qu.: 24406   1st Qu.:1231656  
 Median :2016   Mode  :character   Median : 38898   Median :1426741  
 Mean   :2016                      Mean   : 43136   Mean   :1442396  
 3rd Qu.:2019                      3rd Qu.: 55572   3rd Qu.:1915367  
 Max.   :2021                      Max.   :101915   Max.   :2496146  
ict_stats$Quarter <- as.factor(ict_stats$Quarter)
head(ict_stats)
A data.frame: 6 × 4
YearQuarterADSLMobile
<int><fct><int><int>
12011Q114082189803
22011Q214419200198
32011Q314474224474
42011Q415707238942
52012Q116298263131
62012Q217204294548
library(dplyr)
library(viridis)
library(ggplot2)
library(plotly)
library(gridExtra)
par(mfrow=c(2,1))

g1 <- ggplot(ict_stats, aes(x = Year, y = ADSL, color = Quarter)) + 
geom_line() + 
scale_y_continuous(labels = scales::unit_format(scale = 1e-6)) +
ggtitle('ADSL Subscriptions') +
theme(plot.title = element_text(hjust = 0.5)) +
scale_color_viridis(discrete = TRUE)

g2 <- ggplot(ict_stats, aes(x = Year, y = Mobile, color = Quarter)) + 
geom_line() + 
scale_y_continuous(labels = scales::unit_format(scale = 1e-6)) +
ggtitle('Mobile Phone Internet Subscriptions') +
theme(plot.title = element_text(hjust = 0.5)) +
scale_color_viridis(discrete = TRUE) 

ggplotly(g1)
ggplotly(g2)
#colours
colors <- viridis(8, option = 'A')
#show_col(colors)

#First generate accumulated dataframe
accumulate_by <- function(dat, var) {
  var <- lazyeval::f_eval(var, dat)
  lvls <- plotly:::getLevels(var)
  dats <- lapply(seq_along(lvls), function(x) {
    cbind(dat[var %in% lvls[seq(1, x)], ], frame = lvls[[x]])
  })
  dplyr::bind_rows(dats)
}

df <- ict_stats %>%
filter( Quarter == 'Q4') %>%
accumulate_by(~Year)

g <- ggplot(df, aes(x = Year, frame = frame)) +
geom_line(aes(y = ADSL, colour='ADSL')) +
geom_line(aes(y = Mobile, colour='Mobile')) +
geom_point(aes(y = ADSL, colour='ADSL')) +
geom_point(aes(y = Mobile, colour='Mobile')) +
scale_y_continuous(labels = scales::unit_format(scale = 1e-6)) +
scale_color_manual(values = c(colors[3],colors[5]), name = 'Type') +
ggtitle('Number of Internet Subscriptions') +
ylab('Subscriptions') + 
theme(plot.title = element_text(hjust = 0.5))

g <- g %>% animation_opts(
  frame = 1000, 
  transition = 5
)

g <- g %>% animation_slider(
  currentvalue = list(
    prefix = "Year "
  )
)
    
ggplotly(g)
#head(ict_stats)
adsl <- ict_stats %>%
filter(Quarter == 'Q4') %>%
select(Year, ADSL) %>%
rename(Subscription = ADSL) %>%
mutate(Type = 'ADSL') 

mobile <- ict_stats %>%
filter(Quarter == 'Q4') %>%
select(Year,Mobile) %>%
rename(Subscription = Mobile) %>%
mutate(Type = 'Mobile')

internet_subs <- rbind(adsl,mobile)


#head(adsl)
#head(mobile)
str(internet_subs)
'data.frame':	22 obs. of  3 variables:
 $ Year        : int  2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 ...
 $ Subscription: int  15707 18838 27819 27867 36845 41833 48901 55390 62058 77353 ...
 $ Type        : chr  "ADSL" "ADSL" "ADSL" "ADSL" ...
colors <- viridis(8)
#show_col(colors)

g <- internet_subs %>%
ggplot(aes(x = Year, y = Subscription, fill = Type, aplha = 0.5)) +
geom_bar(stat="identity", position="dodge") +
scale_y_continuous(labels = scales::unit_format(scale = 1e-6)) +
scale_fill_manual(values = c(colors[8],colors[6])) +
ggtitle('ADSL and Mobile Internet Subscriptions') +
ylab('Subscriptions') + 
theme(plot.title = element_text(hjust = 0.5)) 
#geom_text(size = 3, position = position_dodge(width = .9), color = colors[3])

ggplotly(g, tooltip = c("Year", "Subscription", "Type"))